package com.wwd.statemachine.configuration;

import com.wwd.statemachine.enums.OrderFactoryStatus;
import com.wwd.statemachine.enums.OrderFactoryStatusChangeEvent;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.config.EnableStateMachineFactory;
import org.springframework.statemachine.config.StateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
import org.springframework.statemachine.listener.StateMachineListener;
import org.springframework.statemachine.listener.StateMachineListenerAdapter;
import org.springframework.statemachine.state.State;
import org.springframework.statemachine.transition.Transition;

import java.util.EnumSet;

/**
 * @author wangweidong
 * @date 2025-06-24 16:26
 * @description 状态机工厂配置   StateMachine StateMachine
 * 是否生成单例状态机	✅ 是（默认）	❌ 否（每次获取都是新实例）
 * 适合场景	单个状态机	多个状态机（动态创建）
 * 获取方式	注入 StateMachine	注入 StateMachine
 * 是否支持配置类	✅ 支持	✅ 支持
 * 使用复杂度	简单	稍复杂
 */
@Configuration
@EnableStateMachineFactory(name = "orderStateMachineFactory")
public class OrderStateMachineFactoryConfig extends StateMachineConfigurerAdapter<OrderFactoryStatus, OrderFactoryStatusChangeEvent> {
    @Override
    public void configure(StateMachineConfigurationConfigurer<OrderFactoryStatus, OrderFactoryStatusChangeEvent> config) throws Exception {
        config.withConfiguration()
                .autoStartup(true)
//                .machineId("orderStateMachineFactory")
//                .listener(listener())
        ;
    }

    /**
     * 配置状态
     *
     * @param states
     * @throws Exception
     */
    public void configure(StateMachineStateConfigurer<OrderFactoryStatus, OrderFactoryStatusChangeEvent> states) throws Exception {
        states
                .withStates()
                .initial(OrderFactoryStatus.WAIT_PAYMENT)
                .states(EnumSet.allOf(OrderFactoryStatus.class));
    }
    /**
     * 配置状态转换事件关系
     *
     * @param transitions
     * @throws Exception
     */
    public void configure(StateMachineTransitionConfigurer<OrderFactoryStatus, OrderFactoryStatusChangeEvent> transitions) throws Exception {
        transitions
                //支付事件:待支付-》待发货
                .withExternal().source(OrderFactoryStatus.WAIT_PAYMENT).target(OrderFactoryStatus.WAIT_DELIVER).event(OrderFactoryStatusChangeEvent.PAYED)
                .and()
                //发货事件:待发货-》待收货
                .withExternal().source(OrderFactoryStatus.WAIT_DELIVER).target(OrderFactoryStatus.WAIT_RECEIVE).event(OrderFactoryStatusChangeEvent.DELIVERY)
                .and()
                //收货事件:待收货-》已完成
                .withExternal().source(OrderFactoryStatus.WAIT_RECEIVE).target(OrderFactoryStatus.FINISH).event(OrderFactoryStatusChangeEvent.RECEIVED);
    }

    @Bean
    public StateMachineListener<OrderFactoryStatus, OrderFactoryStatusChangeEvent> listener() {
        return new StateMachineListenerAdapter<>() {
            @Override
            public void stateChanged(State<OrderFactoryStatus, OrderFactoryStatusChangeEvent> from, State<OrderFactoryStatus, OrderFactoryStatusChangeEvent> to) {
                System.out.println("State change to " + to.getId());
            }

            @Override
            public void transition(Transition<OrderFactoryStatus, OrderFactoryStatusChangeEvent> transition) {
                System.out.println("transition " +transition);
            }
        };
    }
}
